package proj;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.*;

import proj.AbstractSyntax.*;
import proj.parser.Parser;
import proj.parser.ParseException;
import proj.Interpreter.*;

/**
* @author Deeandrea Burgos (Fall 2011)
*
* @brief Where the user's program is checked and ran from. 
*	 Coordinates Symbol Table output, followed by
*	 the program output, last the Runtime Stack output (if MODE = 'true')
*/

public class Hmm {

    private static boolean MODE= true;//Change to false if DONT want to display runTimeStack    

    public static void main(String args[]) throws ParseException, IOException, InterpreterRuntimeError {
	// If there is a command line argument, interpret it as a stdin.
        InputStream inStream = System.in;
        if (args.length > 0 ) {
          inStream = new FileInputStream(args[0]);
        }

        Parser parser = new Parser(inStream);
        Program prog = parser.Program();

	String mode = "OFF";//'mode' used for output purposes
	   
	if(MODE == true){

		mode = "ON";
	}
	
	System.out.print("\nRun-Time-Stack is:" + mode);
	   
        StaticTypeCheck staticTypeCheck = new StaticTypeCheck();
        staticTypeCheck.checkProgram(prog);
        
        prog.display();
        
        if (staticTypeCheck.getErrLogger().hasErrors()) {
        	System.err.println("ABORTING EXECUTION DUE TO THE ERRORS");
        } else {
	    
	    System.out.println("*****Program Output (If any):\n ");

            Interpreter interpreter = new Interpreter(MODE);//This is where runTimeS is turned on/off
	    interpreter.setLoopMode();

            interpreter.runProgram(prog);
	    System.out.println("\n*****Program Was Executed\n\n");

	    //If runtime Stack ON/true then print it
	    if(MODE == true){
		//'Calling Interpreter' bc runtime Stack is updated in Interpreter.java
		System.out.println("Calling Interpreter");
		interpreter.getTheStack();//Prints stack after program output is outputted (if Any)
	    }
	    
        }
      }

}
